var util = require('../../utils/util.js')
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    lastX: 0,     //滑动开始x轴位置
    lastY: 0,     //滑动开始y轴位置
    text: "没有滑动",
    currentGesture: 0, //标识手势
    curNav: 3, //当前导航
    disabled: false,
  },

  //滑动移动事件
  handletouchmove: function (event) {
    var currentX = event.touches[0].pageX
    var currentY = event.touches[0].pageY
    var tx = currentX - this.data.lastX
    var ty = currentY - this.data.lastY
    var text = ""
    //左右方向滑动
    if (Math.abs(tx) > Math.abs(ty)) {
     if (tx < 0)
      text = "向左滑动"
     else if (tx > 0)
      text = "向右滑动"
    }
    //上下方向滑动
    else {
     if (ty < 0)
      text = "向上滑动"
     else if (ty > 0)
      text = "向下滑动"
    }

 
    //将当前坐标进行保存以进行下一次计算
    this.data.lastX = currentX
    this.data.lastY = currentY
    this.setData({
     text: text,
    });
   },
  
   //滑动开始事件
   handletouchtart: function (event) {
    this.data.lastX = event.touches[0].pageX
    this.data.lastY = event.touches[0].pageY
   },
   //滑动结束事件
   handletouchend: function (event) {
    this.data.currentGesture = 0;
    var lastX = this.data.lastX
    console.log('x:'+lastX)
    if(lastX>50){
     // util.move_skip('about')
    }
    this.setData({
      lastX: 0,
    });
   },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    load_index(that)
  },
  callPhone:function(e){
    var tel = e.currentTarget.dataset.tel
    wx.makePhoneCall({
     phoneNumber: tel //仅为示例，并非真实的电话号码
    })
    },
   

    saveData: function(e) {
      let _this = this,
        values = e.detail.value
  
  
      // 记录formId
      util.saveFormId(e.detail.formId);
       console.log(values)
      // 表单验证
      if (!_this.validation(values)) {
        util.showError(_this.data.error);
        return false;
      }
      
      wx.showLoading({
        title: '正在提交中...',
      })
      
  

      var url_wx_update = util.getApiUrl(app, "/Contact/feedback", "&lan=en")
      var post_data_wx = {

        "realname":values.realname,
        "phone":values.phone,
        "email":values.email,
        "address":values.address,
        "detail":values.detail
      }
    
      util._post_from(url_wx_update, post_data_wx, function (res_update) {
        //更新
        console.log(res_update)
        if (res_update.data.code == '200') {
          //util.showNotice('提交成功')
          wx.showToast({
            title: '提交成功！',
            duration: 2000
          })
    
          setTimeout(function () {
            wx.redirectTo({
              url: '/pages/lianxi/lianxi'
            })
            /*
            wx.switchTab({
              url: '/pages/wmember/index'
            })
            */
          }, 2000)
          //wx.hideLoading()
    
        } else {
          util.showNotice(res_update.data.msg)
          wx.hideLoading()
          _this.setData({
            disabled: false
           }) 
        }
      })
  
       // 按钮禁用
       _this.setData({
        disabled: true
      });
   
    },
  validation: function(values) {
		var _this = this
	
	 if (values.realname === '') {
		 this.data.error = '请输入您的称呼';
		 return false;
   }
   if (values.phone === '') {
    this.data.error = '请输入您的电话';
    return false;
  }
  if (values.detail === '') {
    this.data.error = '请输入您的诉求';
    return false;
  }

	 return true;
 },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})



function load_index(that) {
  var request_banner_url = util.getApiUrl(app, "/Contact/index", "&lan=en")
  util._get(request_banner_url,
    function (res) {
      var model = res.data.data
      console.log(model)
      that.setData({
        model: model

      })
      wx.setNavigationBarTitle({
        title: model.title
      })
    },
    function (res) {
      util.showToast(res.errMsg)
    }
  )
}